package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        Scanner scanner = new Scanner(System.in);
        int count = bookList.getUseSize();
        //判断数组是否满了
        if ( count == bookList.getBooks().length ){//bookList.getBooks().length获取BookList中图书数组books的总长度
            System.out.println("书架上的书已经满了");
            return;
        }
        //判断是否有这本书
        System.out.println("请输入书名:");
        String name = scanner.nextLine();
        int i = 0;
        for ( i = 0; i < count ; i++) {
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)){
                System.out.println("已经有这本书啦");
                return;
            }
        }
        //书架上没有这个数，则可以输入图书信息
        System.out.println("请输入作者: ");
        String author = scanner.nextLine();
        //如果先价格在类型，nextInt()只读取数值，后面的换行符 nextLine()会读取。
        System.out.println("请输入类型");
        String type = scanner.nextLine();
        System.out.println("请输入价格:");
        int price = scanner.nextInt();
        System.out.println("添加成功");
        //创建图书对象，并插入书架
        Book book = new Book(name,author,price,type);//根据输入的信息，实例化一个具体的 Book 对象，以便后续将其加入书架管理系统。
        bookList.setBooks(count,book);
        bookList.setUseSize(count+1);
        System.out.println(name+"添加成功");


    }
}
